package com.ikangtai.shecare.common.al;

import com.ikangtai.shecare.common.util.AppLog;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DayUnitDSOutput {

    // 备孕
    public static final String PERIOD_YJQ = "yjq"; //1，月经期
    public static final String PERIOD_LPQ = "lpq"; //2，卵泡期
    public static final String PERIOD_PLQ = "plq"; //3，排卵期
    public static final String PERIOD_PLR = "plr"; //4，排卵日
    public static final String PERIOD_HTQ = "htq"; //5，黄体期

    // 避孕
    public static final String PERIOD_JHAQQ = "aqq"; // 2，经后安全期, 目前统一为安全期
    public static final String PERIOD_WXQ = "wxq"; // 3， 危险期
    public static final String PERIOD_JQAQQ = "aqq"; //4， 经前安全期, 目前统一为安全期
    public static final String PERIOD_AQQ = "aqq";

    public static final String PERIOD_CONFIRM_YJQ = "confirm";
    public static final String PERIOD_FORECAST_YJQ = "forecast";

    public int periodAchieveForecast; // 备孕算法，预测所处生理期
    public int periodAchieveConfirm; // 备孕算法，确定所处生理期
    public int periodAvoidForecast; // 避孕算法，预测所处生理期
    public int periodAvoidConfirm; // 避孕算法，确定所处生理期
    public int homePageMenstruationEnd; // 首页显示信息：距离月经结束X天
    public int homePageOvulation; // 首页显示信息：距离排卵X天
    public float homePageConceptionChance; // 首页显示信息：受孕概率X%
    public int homePageNextMenstruation; // 首页显示信息：距离下次月经X天
    public long date; // 当天日历日期，含年月日, 秒数seconds
    public int dayOfCycle; // 当天为该周期中第X天

	public DayUnitDSOutput(){
		this.dayOfCycle = 0;
		this.date = 0;
		this.periodAchieveConfirm = 0;
		this.periodAchieveForecast = 0;
		this.periodAvoidConfirm = 0;
		this.periodAvoidForecast = 0;
		this.homePageConceptionChance = 0;
		this.homePageMenstruationEnd = 0;
		this.homePageOvulation = 0;
		this.homePageMenstruationEnd = 0;
	}

	public void setData(int x){
		this.dayOfCycle = x;
		this.date = x;
		this.periodAchieveConfirm = x;
		this.periodAchieveForecast = x;
		this.periodAvoidConfirm = x;
		this.periodAvoidForecast = x;
		this.homePageConceptionChance = x;
		this.homePageMenstruationEnd = x;
		this.homePageOvulation = x;
		this.homePageMenstruationEnd = x;
	}

	public String getPeriod(int state){
        String period = null;

        switch (state) {
            case 0:
                period = getBeiYunPeriod();
                break;
            case 1:
                period = getBiYunPeriod();
                break;
            default:
                AppLog.i("unExpected state!");
        }

        return period;
    }

    private String getBeiYunPeriod(){
        if (periodAchieveConfirm > 0)
            return getBeiYunPeriod("Confirm");
        else if(periodAchieveForecast > 0)
            return getBeiYunPeriod("Forecast");
        else{
            AppLog.i("unExpected AL dayOutput period in STATUS_BEIYUN state!");
            return null;
        }
    }

    private String getBeiYunPeriod(String confirmOrForcast){
        String period = null;
        int periodConfirmOrForcast = 0;

        if(confirmOrForcast.equals("Confirm"))
            periodConfirmOrForcast = periodAchieveConfirm;

        if(confirmOrForcast.equals("Forecast"))
            periodConfirmOrForcast = periodAchieveForecast;

        //AppLog.i("getBeiYunPeriod, periodConfirmOrForcast = " + periodConfirmOrForcast);
        switch (periodConfirmOrForcast){
            case 1:
                period = PERIOD_YJQ;
                break;
            case 2:
                period = PERIOD_LPQ;
                break;
            case 3:
                period = PERIOD_PLQ;
                break;
            case 4:
                period = PERIOD_PLR;
                break;
            case 5:
                period = PERIOD_HTQ;
                break;
        }

        return period;
    }

    private String getBiYunPeriod(){
        if (periodAvoidConfirm > 0)
            return getBiYunPeriod("Confirm");
        else if(periodAvoidForecast > 0)
            return getBiYunPeriod("Forecast");
        else{
            AppLog.i("unExpected AL dayOutput period in STATUS_BIYUN state!");
            return null;
        }
    }

    private String getBiYunPeriod(String confirmOrForcast){
        String period = null;
        int periodConfirmOrForcast = 0;

        if(confirmOrForcast.equals("Confirm"))
            periodConfirmOrForcast = periodAvoidConfirm;

        if(confirmOrForcast.equals("Forecast"))
            periodConfirmOrForcast = periodAvoidForecast;

        AppLog.i("getBiYunPeriod, periodConfirmOrForcast = " + periodConfirmOrForcast);

        switch (periodConfirmOrForcast){
            case 1:
                period = PERIOD_YJQ;
                break;
            case 2:
                period = PERIOD_JHAQQ;
                break;
            case 3:
                period = PERIOD_WXQ;
                break;
            case 4:
                period = PERIOD_JQAQQ;
                break;
        }
        return period;
    }

    public String isConfrimOrForecast(int state){
//        String confirmOrForcast = null;
//
//        if(state == 0){
//            confirmOrForcast = (periodAchieveConfirm == 1 ? PERIOD_CONFIRM_YJQ : PERIOD_FORECAST_YJQ);
//        }else{
//            confirmOrForcast = (periodAvoidConfirm == 1 ? PERIOD_CONFIRM_YJQ : PERIOD_FORECAST_YJQ);
//        }
//
//        return confirmOrForcast;

        long currentSeconds = System.currentTimeMillis() / 1000;
        long deltaSeconds = currentSeconds % (24 * 3600);
        long today12OClock = (currentSeconds - deltaSeconds) + (12 * 3600);

        return (date <= today12OClock ? PERIOD_CONFIRM_YJQ : PERIOD_FORECAST_YJQ);
    }

	public void showLog(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String date = sdf.format(new Date(this.date*1000));
		AppLog.i("Algorithm DayUnitDSOutput show -> dayOfCycle:" + this.dayOfCycle + ", date:" + date + ", periodAchieveConfirm:" + this.periodAchieveConfirm +
				", periodAchieveForecast:" + this.periodAchieveForecast +
				", periodAvoidConfirm:" + this.periodAvoidConfirm +
				", periodAvoidForecast:" + this.periodAvoidForecast +
				", homePageConceptionChance:" + this.homePageConceptionChance +
				", homePageMenstruationEnd:" + this.homePageMenstruationEnd +
				", homePageOvulation:" + this.homePageOvulation +
				", homePageMenstruationEnd:" + this.homePageMenstruationEnd);
	}
}
